﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Framework.Infrastructure.Serialization;
using HalconDotNet;

namespace Blob
{
    /// <summary>
    /// CustomWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CustomWindow : Window
    {
        /// <summary>
        /// 创建CustomWindow新实例
        /// </summary>
        /// <param name="visionOperation"></param>
        public CustomWindow(VisionOperation visionOperation)
        {
            InitializeComponent();

            VisionOperation = visionOperation;
            MinThresholdTextBox.Text = VisionOperation.MinThreshold.ToString();
            MaxThresholdTextBox.Text = VisionOperation.MaxThreshold.ToString();
            MinAreaTextBox.Text = VisionOperation.MinArea.ToString();
        }

        /// <summary>
        /// 视觉算子
        /// </summary>
        public VisionOperation VisionOperation { get; set; }

        public HWindow HWindow { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //ContentControl.Content = VisionOperation.RunningWindow;
            //HWindow = 

        }

        private void Window_Closed(object sender, EventArgs e)
        {

        }

        private void SetButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                VisionOperation.MinThreshold = int.Parse(MinThresholdTextBox.Text);
                VisionOperation.MaxThreshold = int.Parse(MaxThresholdTextBox.Text);
                VisionOperation.MinArea = int.Parse(MinAreaTextBox.Text);

                //保存参数
                VisionOperationParam visionOperationParam = new VisionOperationParam();
                visionOperationParam.MinThreshold = VisionOperation.MinThreshold;
                visionOperationParam.MaxThreshold = VisionOperation.MaxThreshold;
                visionOperationParam.MinArea = VisionOperation.MinArea;
                JsonSerialization.SerializeObjectToFile(visionOperationParam, $"{VisionOperation.BaseDirectory}/InputParam/default.json"); ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void RunButton_Click(object sender, RoutedEventArgs e)
        {
            object imageObject = new object();
            //获取图像
            VisionOperation.GetImage?.Invoke(2000, out imageObject);

            HObject image;
            HOperatorSet.GenEmptyObj(out image);
            image?.Dispose();
            if (imageObject is HObject)
            {
                image = imageObject as HObject;
                //ContentControl.Content = VisionOperation.RunningWindow;
                VisionPlatform.BaseType.ItemCollection outputs;
                VisionOperation.Execute(image, HWindowControlWPF.HalconWindow, out outputs);
            }
        }

        private void RunLocalImageButton_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawRectangleButton_Click(object sender, RoutedEventArgs e)
        {
            HTuple r1, r2, c1, c2;
            HOperatorSet.DrawRectangle1(HWindowControlWPF.HalconWindow, out r1, out c1, out r2, out c2);
        }
    }

    public class VisionOperationParam
    {
        /// <summary>
        /// 最小阈值
        /// </summary>
        public int MinThreshold { get; set; } = 0;

        /// <summary>
        /// 最大阈值
        /// </summary>
        public int MaxThreshold { get; set; } = 100;

        /// <summary>
        /// 最小面积
        /// </summary>
        public int MinArea { get; set; } = 1000;
    }
}
